package cn.yuxin123;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xiaomi.ad.SplashAdListener;
import com.xiaomi.ad.adView.SplashAd;

import cn.yuxin123.memory.MainActivity;
import cn.yuxin123.memory.R;
import cn.yuxin123.memory.common.Shared;
import cn.yuxin123.memory.utils.FontLoader;

/**
 * Created by yxx on 2017/4/3.
 */

public class WelcomeActivity extends Activity {

    private RelativeLayout ad;

    private Handler mHandler;

    private static final int MSG_LOGIN = 0x0001;

    private TextView gameTitle;

    private static final String TAG = WelcomeActivity.class.getSimpleName();

    private static final String POSITION_ID = "b373ee903da0c6fc9c9da202df95a500";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.welcome);

        mHandler = new WelcomeHandler();

        //init();
        initSetting();
    }

    private void init() {
        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    Thread.sleep(5000);

                    mHandler.sendEmptyMessage(MSG_LOGIN);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    class WelcomeHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if (msg.what == MSG_LOGIN) {
                Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                startActivity(intent);
            }
        }
    }

    private void initSetting() {

        ad = (RelativeLayout)findViewById(R.id.game_ad);
        gameTitle = (TextView)findViewById(R.id.game_title);

        FontLoader.setTypeface(Shared.context, new TextView[] {gameTitle}, FontLoader.Font.GROBOLD);

        SplashAd splashAd = new SplashAd(this, ad, R.drawable.splash, new SplashAdListener() {
            @Override
            public void onAdPresent() {
                // 开屏广告展示
                Log.d(TAG, "onAdPresent");
            }

            @Override
            public void onAdClick() {
                //用户点击了开屏广告
                Log.d(TAG, "onAdClick");
            }

            @Override
            public void onAdDismissed() {
                //这个方法被调用时，表示从开屏广告消失。
                Log.d(TAG, "onAdDismissed");
                Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }

            @Override
            public void onAdFailed(String s) {
                Log.d(TAG, "onAdFailed, message: " + s);
            }
        });
        splashAd.requestAd(POSITION_ID);

    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            // 捕获back键，在展示广告期间按back键，不跳过广告
            if (ad.getVisibility() == View.VISIBLE) {
                return true;
            }
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
